package ar.edu.unq.wannaplay.services;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import ar.edu.unq.wannaplay.dtos.CareerPlanDTO;
import ar.edu.unq.wannaplay.dtos.CareerPlanOrganizerDTO;
import ar.edu.unq.wannaplay.helpers.CareerPlanOrganizer;
import ar.edu.unq.wannaplay.ocupation.CareerPlan;
import ar.edu.unq.wannaplay.persistence.daos.CareerPlanOrganizerDAO;
import ar.edu.unq.wannaplay.utils.DTOHelper;
import ar.edu.unq.wannaplay.utils.Service;

public class CareerPlanService implements Service<CareerPlan, CareerPlanDTO> {
    private static final long serialVersionUID = 1L;

    private CareerPlanOrganizerDAO dao;

    public CareerPlanOrganizerDAO getDao() {
        return dao;
    }

    public void setDao(final CareerPlanOrganizerDAO dao) {
        this.dao = dao;
    }

    @Transactional
    public void saveCareerPlanOrganizer(final CareerPlanOrganizer organizer) {
        this.getDao().save(organizer);
    }

    @Transactional
    public List<CareerPlanDTO> findAllCareerPlanDto() {
        return this.toDtos(this.getDao().findAllCareerPlans());
    }

    private List<CareerPlanDTO> toDtos(final List<CareerPlan> careerPlans) {
        DTOHelper<CareerPlan, CareerPlanDTO> helper = new DTOHelper<CareerPlan, CareerPlanDTO>();
        return helper.toDtos(careerPlans, this);

    }

    @Transactional
    public CareerPlanOrganizerDTO getCareerPlanOrganizer() {
        return new CareerPlanOrganizerDTO(this.getDao().getUnique());
    }

    @Transactional
    public void save(final String careerPlanName) {
        this.getDao().saveCareerPlan(careerPlanName);
    }

    @Transactional
    public void updateSalaries(final CareerPlanDTO careerPlan) {
        this.getDao().updateCareerPlanSalaries(careerPlan.fromDto());
    }

    @Transactional
    public void updateName(final CareerPlanDTO careerPlan) {
        this.getDao().updateCareerPlanName(careerPlan.getOldName(), careerPlan.getName());
    }

    @Transactional
    public void delete(final CareerPlanDTO careerPlan) {
        this.getDao().deleteCareerPlan(careerPlan.fromDto());
    }

    @Transactional
    public void updateAddedRank(final CareerPlanOrganizerDTO careerPlanOrganizer) {
        this.getDao().addRank(careerPlanOrganizer.getNewRank(), careerPlanOrganizer.getNewRanges());
    }

    @Transactional
    public void updateModifyedRankName(final CareerPlanOrganizerDTO careerPlanOrganizer) {
        this.getDao().modifyRankName(careerPlanOrganizer.getModifyedRank(), careerPlanOrganizer.getNewRank());
    }

    @Transactional
    public void updateModifyRanges(final CareerPlanOrganizerDTO careerPlanOrganizer) {
        this.getDao().modifyRanges(careerPlanOrganizer.getModifyedRank(), careerPlanOrganizer.getNewRanges());
    }

    @Transactional
    public void updateDeletedRank(final CareerPlanOrganizerDTO careerPlanOrganizer) {
        this.getDao().removeRank(careerPlanOrganizer.getDeletedRank());
    }

    @Override
    public CareerPlanDTO toDto(final CareerPlan careerPlan) {
        return new CareerPlanDTO(careerPlan);

    }

}
